package top.drinyeit.nongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.adapter.ShopBottomGoodsAdapter;
import top.drinyeit.nongmao.adapter.ShopCenterClassfyAdapter;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.interfaces.OnItemClickListener;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.view.dropdown.DropDownMenu;

/**
 * Created by bigHead on 2017/11/17.
 */

public class GoodsShopClassificationActivity extends BaseActivity {

    private RecyclerView rv_goods_shop_classfycation_list;
    private DropDownMenu mDropDownMenu_goods_shopclassfycation;
    private String headers[] = {"商家分类", "智能排序", "优惠活动"};
    private String citys[] = {"不限", "武汉", "北京", "上海", "成都", "广州", "深圳", "重庆", "天津", "西安", "南京", "杭州"};
    private String ages[] = {"不限", "18岁以下", "18-22岁", "23-26岁", "27-35岁", "35岁以上"};
    private String constellations[] = {"不限", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"};
    private int[] types = new int[]{DropDownMenu.TYPE_LIST_SHOP, DropDownMenu.TYPE_LIST_SORT, DropDownMenu.TYPE_ACTIVITY};
    private ShopCenterClassfyAdapter centerAdapter;
    private ShopBottomGoodsAdapter bottomAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_shopclassfycation);
        setToolbar2(true, getIntent().getStringExtra("title"));
        initView();
        initDropDownListData();
        initRecyclerViewData();
    }

    private void initView() {
        rv_goods_shop_classfycation_list = findViewById(R.id.rv_goods_shop_classfycation_list);
        mDropDownMenu_goods_shopclassfycation = findViewById(R.id.mDropDownMenu_goods_shopclassfycation);
    }

    private void initDropDownListData() {
        mDropDownMenu_goods_shopclassfycation.setDropDownMenu(Arrays.asList(headers), initViewData(), rv_goods_shop_classfycation_list);
        //该监听回调只监听默认类型，如果是自定义view请自行设置，参照demo
        mDropDownMenu_goods_shopclassfycation.addMenuSelectListener(new DropDownMenu.OnDefultMenuSelectListener() {
            @Override
            public void onSelectDefaultMenu(int index, int pos, String clickstr) {
                //index:点击的tab索引，pos：单项菜单中点击的位置索引，clickstr：点击位置的字符串
            }
        });
        final String[] datas = {"选项1", "选项2", "选项3", "选项4", "选项5"};
        mDropDownMenu_goods_shopclassfycation.setOnMyClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: 2016/5/17 构建一个popupwindow的布局
                View popupView = getLayoutInflater().inflate(R.layout.popupwindow, null);

                // TODO: 2016/5/17 为了演示效果，简单的设置了一些数据，实际中大家自己设置数据即可，相信大家都会。
                ListView lsvMore = popupView.findViewById(R.id.lsvMore);
                lsvMore.setAdapter(new ArrayAdapter<String>(GoodsShopClassificationActivity.this, android.R.layout.simple_list_item_1, datas));

                // TODO: 2016/5/17 创建PopupWindow对象，指定宽度和高度
                PopupWindow window = new PopupWindow(popupView, mDropDownMenu_goods_shopclassfycation.getTabWidth(), 600);
                // TODO: 2016/5/17 设置动画
//                window.setAnimationStyle(R.style.popup_window_anim);
                // TODO: 2016/5/17 设置背景颜色
                window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
                // TODO: 2016/5/17 设置可以获取焦点
                window.setFocusable(true);
                // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
                window.setOutsideTouchable(true);
                // TODO：更新popupwindow的状态
                window.update();
                // TODO: 2016/5/17 以下拉的方式显示，并且可以设置显示的位置
                window.showAsDropDown(view, 0, 20);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        mDropDownMenu_goods_shopclassfycation.closeMenu();
                    }
                });
            }
        });
    }

    private List<HashMap<String, Object>> initViewData() {
        List<HashMap<String, Object>> viewDatas = new ArrayList<>();
        HashMap<String, Object> map;
        for (int i = 0; i < headers.length; i++) {
            map = new HashMap<String, Object>();
            map.put(DropDownMenu.KEY, types[i]);
            switch (types[i]) {
                case DropDownMenu.TYPE_LIST_SHOP:
                    map.put(DropDownMenu.VALUE, citys);
                    map.put(DropDownMenu.SELECT_POSITION, 0);
                    break;
                case DropDownMenu.TYPE_LIST_SORT:
                    map.put(DropDownMenu.VALUE, ages);
                    map.put(DropDownMenu.SELECT_POSITION, 0);
                    break;
                case DropDownMenu.TYPE_ACTIVITY:
                    map.put(DropDownMenu.VALUE, constellations);
                    break;
            }
            viewDatas.add(map);
        }
        return viewDatas;
    }

    @Override
    public void init() {
    }

    private void initRecyclerViewData() {
        List<CategoryGoodsDetailsBean> array = new ArrayList<>();
        rv_goods_shop_classfycation_list.setLayoutManager(new LinearLayoutManager(this));
        rv_goods_shop_classfycation_list.setNestedScrollingEnabled(true);
        centerAdapter = new ShopCenterClassfyAdapter(this, array);
        rv_goods_shop_classfycation_list.setAdapter(centerAdapter);
        centerAdapter.setOnRvItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NotNull View view, int position) {
                startActivity(new Intent(GoodsShopClassificationActivity.this, GoodsShopClassificationActivity.class));
            }
        });
        rv_goods_shop_classfycation_list.setLayoutManager(new LinearLayoutManager(this));
        rv_goods_shop_classfycation_list.setNestedScrollingEnabled(false);        //添加Android自带的分割线
        rv_goods_shop_classfycation_list.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        bottomAdapter = new ShopBottomGoodsAdapter(this, array);
        bottomAdapter.setOnRvItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NotNull View view, int position) {
                startActivity(new Intent(GoodsShopClassificationActivity.this, ShopInnerGoodsActivity.class));
            }
        });
        rv_goods_shop_classfycation_list.setAdapter(bottomAdapter);
//        rv_goods_shop_classfycation_list.setVisibility(View.INVISIBLE);
    }
}
